﻿using System;
using System.Diagnostics;
using System.Threading;
using System.Windows.Forms;

namespace MightyPoke.GUI
{
    public partial class FormDesigner : Form
    {
        private Poke poke;
        private Thread thread;

        public FormDesigner()
        {
            InitializeComponent();

            DriveLabel.Text = StringResources.Label_Drive_Big;
            DriveLabel2.Text = StringResources.Label_Drive_Normal;
            FilenameLabel.Text = StringResources.Label_Filename_Normal;
            IntervalLabel.Text = StringResources.Label_Interval_Big;

            HoursLabel.Text = StringResources.Label_Hours_Normal;
            MinutesLabel.Text = StringResources.Label_Minutes_Normal;
            SecondsLabel.Text = StringResources.Label_Seconds_Normal;
            StartButton.Text = StringResources.Button_Start;
            StopButton.Text = StringResources.Button_Stop;

            LinkLabel.Text = StringResources.Link_Text;
            LinkLabel.Links.Add(0, LinkLabel.Text.Length, StringResources.Link_URL);
        }


        private void StopButton_Click(object sender, EventArgs e)
        {
            StopButton.Visible = false;
            StartButton.Visible = true;
            poke.Stop();
            thread.Abort();
        }

        private void FormDesigner_Resize(object sender, EventArgs e)
        {
            Notifier.BalloonTipTitle = StringResources.Notifier_Title;
            Notifier.BalloonTipText = StringResources.Notifier_Message;

            if (FormWindowState.Minimized == WindowState)
            {
                Notifier.Visible = true;
                Notifier.ShowBalloonTip(500);
                Hide();
            }
            else if (FormWindowState.Normal == WindowState)
            {
                Notifier.Visible = false;
            }
        }

        private void Notifier_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            Show();
            WindowState = FormWindowState.Normal;
        }

        private void StartButton_Click(object sender, EventArgs e)
        {
            string drive = DriveTextBox.Text;

            if (drive == null)
                return;
            if (drive != null && drive.Length > 1)
                drive = drive.Substring(0, 1);


            string file = FilenameTextBox.Text;

            int hours = Convert.ToInt32(HoursComboBox.SelectedItem);
            int minutes = Convert.ToInt32(MinutesComboBox.SelectedItem);
            int seconds = Convert.ToInt32(SecondsComboBox.SelectedItem);

            StartButton.Visible = false;
            StopButton.Visible = true;

            poke = new Poke(drive, file, hours, minutes, seconds);

            thread = new Thread(poke.PokeIt);
            thread.Start();
            while (!thread.IsAlive) ;
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start((string) e.Link.LinkData);
        }
    }
}